package com.example.tool.activtiy.recylerview;

import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemChildLongClickListener;
import com.example.tool.R;
import com.example.tool.activtiy.base.BaseActivity;
import com.example.tool.adapter.SingleAdapter;
import com.example.tool.adapter.base.decoration.RecycleViewDivider;
import com.example.tool.bean.CommonBean;
import com.example.tool.listener.OnBarItemListener;
import com.example.tool.mvp.CommonPresent;
import com.example.tool.util.ToastUtil;
import com.example.tool.widget.PopUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created Date 2019/8/25.
 *
 * @author liangkun
 * ClassDescription：单条目
 */
public class SingleItemRvActivity extends BaseActivity<CommonPresent> {
    @BindView(R.id.rv_single)
    protected RecyclerView rvSingle;
    @BindView(R.id.tv_title)
    protected TextView tvTitle;
    @BindView(R.id.toolbarRightIcon)
    protected ImageView ivMore;
    @BindView(R.id.appbar)
    protected AppBarLayout barLayout;
    private SingleAdapter singleAdapter;
    private LinearLayoutManager linearLayoutManager;
    private List<CommonBean> beans;

    @Override
    protected int getLayoutId() {
        return R.layout.activtiy_single_rv;
    }

    @Override
    protected void initView() {
        tvTitle.setText("单条目view");
        ivMore.setVisibility(View.VISIBLE);
        ivMore.setImageResource(R.mipmap.iv_bar_more);
        linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        rvSingle.addItemDecoration(new RecycleViewDivider(this, LinearLayoutManager.VERTICAL,
                getResources().getDimensionPixelOffset(R.dimen.sw_dp_15), 0, 0, getResources().getColor(R.color.color_white)));
        rvSingle.setLayoutManager(linearLayoutManager);
        singleAdapter = new SingleAdapter(this, getData());
        //默认动画
//        singleAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        singleAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);


        // 自定义动画如此轻松 TODO:保留
//        singleAdapter.openLoadAnimation(new BaseAnimation() {
//            @Override
//            public Animator[] getAnimators(View view) {
//                return new Animator[]{
//                        ObjectAnimator.ofFloat(view, "scaleY", 1f, 1.5f, 1f),
//                        ObjectAnimator.ofFloat(view, "scaleX", 1f, 1.5f, 1f)
//                };
//            }
//        });
        //

        //动画默认只执行一次,如果想重复执行可设置
//        singleAdapter.isFirstOnly(false);
        //添加头部尾部
        View headView = getHeaderView(0, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //点击 ，放在0位，变换删除按钮
                singleAdapter.addHeaderView(getHeaderView(1, getRemoveHeaderListener()), 0);
            }
        });
        singleAdapter.addHeaderView(headView);
        //底部控件添加
        View footView = getFootView(0, new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                singleAdapter.addFooterView(getFootView(1, getRemoveFooterListener()), 0);
            }
        });
        singleAdapter.addFooterView(footView, 0);

        rvSingle.setAdapter(singleAdapter);

    }

    /**
     * foot布局
     *
     * @return
     */
    private View getFootView(int type, View.OnClickListener listener) {
        View inflate = LayoutInflater.from(this).inflate(R.layout.layout_footer_view, (ViewGroup) rvSingle.getParent(), false);
        ImageView imageView = inflate.findViewById(R.id.iv_foot_d);
        if (type == 1) {
            imageView.setImageResource(R.mipmap.iv_delete);
        }
        inflate.setOnClickListener(listener);
//        imageView.setOnClickListener(listener);
        return inflate;
    }

    /**
     * header布局
     *
     * @return
     */
    private View getHeaderView(int type, View.OnClickListener listener) {
        //此处需要rv的 parent
        View inflate = LayoutInflater.from(this).inflate(R.layout.layout_header_view, (ViewGroup) rvSingle.getParent(), false);
        ImageView imageView = inflate.findViewById(R.id.iv_head_d);
        if (type == 1) {
            imageView.setImageResource(R.mipmap.iv_delete);
        }
        //这里可以设置图标点击事件 ,
//        imageView.setOnClickListener(listener);
        inflate.setOnClickListener(listener);
        return inflate;
    }

    /**
     * 移除view
     *
     * @return
     */
    private View.OnClickListener getRemoveHeaderListener() {
        return new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                singleAdapter.removeHeaderView(v);

            }
        };
    }

    private View.OnClickListener getRemoveFooterListener() {
        return new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                singleAdapter.removeFooterView(v);
            }
        };
    }

    private List<CommonBean> getData() {
        beans = new ArrayList<>();
        for (int i = 0; i < 40; i++) {
            CommonBean commonBean = new CommonBean();
            String str = "12560" + i * 100;
            commonBean.setDes(str);
            commonBean.setTitle("诛仙 " + i);
            beans.add(commonBean);
        }
        return beans;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initEvent() {
//        /**
//         * 多种点击
//         */
//        rvSingle.addOnItemTouchListener(new SimpleClickListener() {
//            @Override
//            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                Toast.makeText(SingleItemRvActivity.this, "" + Integer.toString(position), Toast.LENGTH_SHORT).show();
//            }
//
//            @Override
//            public void onItemLongClick(BaseQuickAdapter adapter, View view, int position) {
//                Toast.makeText(SingleItemRvActivity.this, "" + Integer.toString(position), Toast.LENGTH_SHORT).show();
//            }
//
//            @Override
//            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
//                Toast.makeText(SingleItemRvActivity.this, "" + Integer.toString(position), Toast.LENGTH_SHORT).show();
//            }
//
//            @Override
//            public void onItemChildLongClick(BaseQuickAdapter adapter, View view, int position) {
//                Toast.makeText(SingleItemRvActivity.this, "" + Integer.toString(position), Toast.LENGTH_SHORT).show();
//            }
//        });

        /**
         * 条目点击---
         */
        singleAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                ToastUtil.toastShortContent("点击--整个ITEM--哈哈哈 " + position);
            }
        });
        /**
         * 条目点击---
         */
        singleAdapter.setOnItemLongClickListener(new BaseQuickAdapter.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(BaseQuickAdapter adapter, View view, int position) {
                ToastUtil.toastShortContent("长按---整个ITEM-嘿嘿嘿 " + position);
                return false;
            }
        });
        /**
         * 子控件的点击---
         */
        rvSingle.addOnItemTouchListener(new OnItemChildClickListener() {
            @Override
            public void onSimpleItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.tv_click:
                        ToastUtil.toastShortContent("点击子布局--点击我 " + position);
                        break;
                    case R.id.tv_title:
                        ToastUtil.toastShortContent("点击子布局--标题 " + beans.get(position).getTitle());
                        break;

                }
            }

        });
        rvSingle.addOnItemTouchListener(new OnItemChildLongClickListener() {

            @Override
            public void onSimpleItemChildLongClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.tv_click:
                        ToastUtil.toastShortContent("长按 点击我-- 嘿嘿嘿 我");
                        break;
                    case R.id.tv_title:
                        ToastUtil.toastShortContent("长按--标题--嘿嘿嘿 " + beans.get(position).getTitle());
                        break;
                }

            }
        });

        ivMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PopUtil.getInstance().showLeftPop(SingleItemRvActivity.this, ivMore, new OnBarItemListener() {
                    @Override
                    public void OnItemClick(int postion) {
                        switch (postion) {
                            case 0:
                                singleAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);
                                break;
                            case 1:
                                singleAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
                                break;
                            case 2:
                                singleAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_BOTTOM);
                                break;
                            case 3:
                                singleAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_LEFT);
                                break;
                            case 4:
                                singleAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_RIGHT);
                                break;
                        }
                    }
                });
            }
        });

    }

    @Override
    protected CommonPresent createPresenter() {
        return null;
    }
}